package com.example.multi.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.example.multi.entity.DataResp;
import com.example.multi.entity.Resp;
import com.example.multi.entity.User;
import com.example.multi.service.ITestRequestService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Slf4j
@RestController
@RequestMapping(value = "/v1/testUser")
public class TestUserController {

    @Autowired
    ITestRequestService testRequestService;

    @ApiOperation(value = "聚合数据接口(一)-串行获取数据")
    @RequestMapping(value = "/listUser", method = RequestMethod.GET)
    public Resp<List<User>> listUser(@RequestHeader(value = "token",required = false)String token){
        TimeInterval timeInterval = DateUtil.timer();
        DataResp dataResp = testRequestService.getDateResp();

        log.info("聚合数据接口(一)-串行获取数据 总耗时：{}毫秒",timeInterval.interval());
        return Resp.buildDataSuccess(dataResp).setTimeInterval(timeInterval.interval());
    }


    @ApiOperation(value = "聚合数据接口(二)-并行获取数据@Async （子线程获取不到token）")
    @RequestMapping(value = "/listUser2", method = RequestMethod.GET)
    public Resp<List<User>> listUser2(@RequestHeader(value = "token",required = false)String token) throws ExecutionException, InterruptedException {
        TimeInterval timeInterval = DateUtil.timer();
        DataResp dataResp = testRequestService.getDateResp2();

        log.info("聚合数据接口(二)-并行获取数据@Async （子线程获取不到token） 总耗时：{}毫秒",timeInterval.interval());
        return Resp.buildDataSuccess(dataResp).setTimeInterval(timeInterval.interval());
    }

    @ApiOperation(value = "聚合数据接口(三)-并行获取数据（子线程能获取到token）")
    @RequestMapping(value = "/listUser3", method = RequestMethod.GET)
    public Resp<List<User>> listUser3(@RequestHeader(value = "token",required = false)String token) throws ExecutionException, InterruptedException {
        TimeInterval timeInterval = DateUtil.timer();
        DataResp dataResp = testRequestService.getDateResp3();

        log.info("聚合数据接口(三)-并行获取数据（子线程能获取到token） 总耗时：{}毫秒",timeInterval.interval());
        return Resp.buildDataSuccess(dataResp).setTimeInterval(timeInterval.interval());
    }




    @ApiOperation(value = "聚合数据接口(四)-并行获取数据@Async （子线程 能透传 请求头信息）")
    @RequestMapping(value = "/listUser4", method = RequestMethod.GET)
    public Resp<List<User>> listUser4(@RequestHeader(value = "token",required = false)String token) throws ExecutionException, InterruptedException {
        TimeInterval timeInterval = DateUtil.timer();
        DataResp dataResp = testRequestService.getDateResp4();

        log.info("聚合数据接口(四)-并行获取数据@Async （子线程 能透传 请求头信息） 总耗时：{}毫秒",timeInterval.interval());
        return Resp.buildDataSuccess(dataResp).setTimeInterval(timeInterval.interval());
    }
}
